package com.rico.deploy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rico.deploy.entity.Academic;
import com.rico.deploy.entity.Campus;
import com.rico.deploy.entity.Classroom;
import com.rico.deploy.entity.ClassroomType;
import com.rico.deploy.mapper.AcademicMapper;
import com.rico.deploy.mapper.CampusMapper;
import com.rico.deploy.mapper.ClassroomMapper;
import com.rico.deploy.mapper.ClassroomTypeMapper;
import com.rico.deploy.service.ClassroomService;
import com.rico.deploy.vo.ClassroomVO;
import com.rico.deploy.vo.DtreeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 吴泽胜
 * @FileName ClassroomServiceImpl
 * @date 2021-05-07 5:14 下午
 * @Software: IntelliJ IDEA
 */
@Service
@Slf4j
public class ClassroomServiceImpl implements ClassroomService {

    @Autowired
    private ClassroomMapper classroomMapper;

    @Autowired
    private AcademicMapper academicMapper;

    @Autowired
    private ClassroomTypeMapper classroomTypeMapper;

    @Autowired
    private CampusMapper campusMapper;

    /**
     * 生成左边树
     * @return
     */
    @Override
    public  List<DtreeVo> LeftTreeView() {
        // 创建插件数据格式
        List<DtreeVo> dtreeVos = new ArrayList<>();
        // 校区
        List<Campus> campuses = campusMapper.selectAll();
        for ( Campus campus : campuses ){
            DtreeVo dtreeVo = new DtreeVo();
            dtreeVo.setId(campus.getCampusCode());  // id
            dtreeVo.setTitle(campus.getCampusName()); // 名称
            dtreeVo.setCheckArr("0");
            dtreeVo.setParentId("0");
            dtreeVos.add(dtreeVo);
        }
        // 教学楼
        List<Academic> academics = academicMapper.selectAll();
        for ( Academic academic : academics ){
            DtreeVo dtreeVo = new DtreeVo();
            dtreeVo.setId("0"+academic.getAcademic_code()); // id
            dtreeVo.setTitle(academic.getAcademic_name()); // 名称
            dtreeVo.setParentId(academic.getCampus_code()); // 父类id
            dtreeVo.setCheckArr("0");
            dtreeVos.add(dtreeVo);
        }
        return dtreeVos;
    }

    @Override
    public List<ClassroomVO> selectAll(Integer page, Integer limit, String keyword) {

        // 教学楼
        List<Academic> academics = academicMapper.selectAll();
        // 教室类型
        List<ClassroomType> classroomTypes = classroomTypeMapper.selectAll();

        // 创建插件数据格式
        List<ClassroomVO> classroomVOS = new ArrayList<>();

        String academic_code = "";
        // 通过 名字 搜索到 code

        for ( Academic academic : academics ){
            if ( academic.getAcademic_name().equals(keyword)){
                academic_code = academic.getAcademic_code();
                break;
            }
        }

        // 通过 code 搜索到 名字
        Example example = new Example(Classroom.class);
        example.createCriteria().andEqualTo("academicCode", academic_code);
        PageHelper.startPage(page,limit);
        List<Classroom> classrooms = classroomMapper.selectByExample(example);

        for ( Classroom classroom : classrooms ){
            ClassroomVO classroomVO = new ClassroomVO();
            classroomVO.setId(classroom.getId());   // id
            classroomVO.setClassroomCode(classroom.getClassroomCode()); // 教室代码
            classroomVO.setClassroomName(classroom.getClassroomName()); // 教室名称
            // 学院
            for ( Academic academic : academics ){
                if (academic.getAcademic_code().equals(classroom.getAcademicCode())){
                    classroomVO.setCampusCode(academic.getCampus_code());       // 校区代码
                    classroomVO.setCampusName(academic.getCampus_name());       // 校区名称
                    classroomVO.setAcademicCode(academic.getAcademic_code());   // 学院代码
                    classroomVO.setAcademicName(academic.getAcademic_name());   // 学院名称
                    break;
                }
            }
            // 教室类型
            for ( ClassroomType classroomType : classroomTypes ){
                if (classroomType.getId().equals(classroom.getClassroomTypeId())){
                    classroomVO.setTypeName(classroomType.getTypeName());
                    break;
                }
            }
            classroomVOS.add(classroomVO);
        }
        return classroomVOS;
    }

    @Override
    public long selectCount(String keyword) {
        // 学院
        List<Academic> academics = academicMapper.selectAll();

        String academic_code = null;
        // 通过 名字 搜索到 code
        for ( Academic academic : academics ){
            if ( academic.getAcademic_name().equals(keyword)){
                academic_code = academic.getCampus_code();
                break;
            }
        }

        // 通过 code 搜索到 名字
        Example example = new Example(Classroom.class);
        example.createCriteria().andEqualTo("academicCode", academic_code);
        List<Classroom> classrooms = classroomMapper.selectByExample(example);
        PageInfo<Classroom> pageInfo = new PageInfo(classrooms);
        return pageInfo.getTotal();
    }

    @Override
    public Classroom selectById(Integer id) {
        return classroomMapper.selectByPrimaryKey(id);
    }

    @Override
    public int insertClassroom(Classroom classroom) {
        return classroomMapper.insert(classroom);
    }

    @Override
    public int deleteClassroomById(Integer id) {
        return classroomMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updateClassroom(Classroom classroom) {
        return classroomMapper.updateByPrimaryKey(classroom);
    }
}
